package com.hss.common.dialogs;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hss.common.R;

/**
 * 作者：黄赛赛 on 2015/10/19 15:27
 *
 * @version V1.0
 * @Description: ${todo}()
 */
public class LoadingDialogFragment extends DialogFragment {

    String msg;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        int style = DialogFragment.STYLE_NORMAL, theme = 0;
//
//        style=  DialogFragment.STYLE_NO_TITLE | DialogFragment.STYLE_NO_FRAME;
//
//        setStyle(style, theme);

        setStyle(DialogFragment.STYLE_NORMAL, R.style.lodDialog);
    }

    ImageView iv_loding;
    TextView tv_loading_txt;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.dialogfragment_loading, container, false);
        iv_loding = (ImageView) v.findViewById(R.id.iv_loding);
        tv_loading_txt = (TextView) v.findViewById(R.id.tv_loading_txt);

        return v;
    }



    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments() != null){
            msg = getArguments().getString("msg");
        }
        if (TextUtils.isEmpty(msg)){
            tv_loading_txt.setVisibility(View.GONE);

        }else {
            tv_loading_txt.setVisibility(View.VISIBLE);
            tv_loading_txt.setText(msg);

        }
    }

    public  void updateMsg(String msg){
        this.msg = msg;
        if (tv_loading_txt != null){
            tv_loading_txt.setText(this.msg == null ? "" : this.msg);
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        if (iv_loding != null){
            AnimationDrawable animationDrawable = (AnimationDrawable) iv_loding.getDrawable();
            animationDrawable.start();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (iv_loding != null){
            AnimationDrawable animationDrawable = (AnimationDrawable) iv_loding.getDrawable();
            animationDrawable.stop();
        }
    }


}
